import os.path
from typing import Optional

from conf.db_config import DbSqliteConfig
from conf.env import db_path
from db.dao.device_dao import DeviceDao
from db.dao.slave_dao import SlaveDao
from db.model.base import Base
from db.model.device import Device
from db.model.register import Register
from db.model.slave import Slave


class DbController:
    device = Device()
    slave = Slave()
    register = Register()

    def __init__(self, is_create_db: bool = False) -> None:
        self.db_config: Optional[DbSqliteConfig] = DbSqliteConfig()
        self.device_dao: Optional[DeviceDao] = DeviceDao(self.db_config)
        self.slave_dao: Optional[SlaveDao] = SlaveDao(self.db_config)
        self.db_config.create_base(Base)
        self.connect_sqlite_db(is_create_db=is_create_db)

    def connect_sqlite_db(self, is_create_db=False):
        self.db_config.init_db(db_path=db_path, is_create_db=is_create_db)


db_controller = DbController()
